from rest_framework import serializers

from plumes.models import Article
from qas.models import Question, Label, Reply
from users.models import User


class UserSimpleArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title')

class ArticleUserSerializer(serializers.ModelSerializer):
    articles = UserSimpleArticleSerializer(label='文章作者对应的文章信息', many=True)
    fans = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')

class ArticleListSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(label='文章对应的作者信息')
    collected_users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    collected = serializers.BooleanField(read_only=True, default=False)

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime', 'user', 'collected_users', 'image', 'collected')
class QuestionSerializerCreat(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = '__all__'


class LabelSerializer(serializers.ModelSerializer):
    # 标签序列化器类
    class Meta:
        model = Label
        fields = ('id', 'label_name')


class QuestionSerializerForList(serializers.ModelSerializer):

    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    class Meta:
        model = Question
        fields = ('id', 'createtime', 'labels', 'solve', 'reply', 'replyname', 'replytime', "title", "unuseful_count", "useful_count", "user", "visits")


class LabelSerializerWithQuestionAndArticle(serializers.ModelSerializer):
    questions = QuestionSerializerForList(many=True, read_only=True)
    articles = ArticleListSerializer(many=True, read_only=True)

    class Meta:
        model = Label
        fields = "__all__"


class QusetionUsersSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')

class QuestionSubsSerializer(serializers.ModelSerializer):
    user = QusetionUsersSerializer(read_only=True)

    class Meta:
        model = Question
        fields = ('id', 'content', 'createtime', 'useful_count', 'unuseful_count', 'user')

class CommentsQuestionSerializer(serializers.ModelSerializer):
    subs = QuestionSubsSerializer(read_only=True, many=True)
    user = QusetionUsersSerializer(read_only=True)
    parent = QuestionSubsSerializer(read_only=True)
    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]

class QuestionSerializerForDetail(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    comment_question = CommentsQuestionSerializer(read_only=True, many=True)
    comment_reply = CommentsQuestionSerializer(read_only=True, many=True)
    answer_question = CommentsQuestionSerializer(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits","content","comment_question","comment_reply","answer_question"]

class ReplySerializerForCreate(serializers.ModelSerializer):

    class Meta:
        model = Reply
        fields = '__all__'

